import { reject } from 'lodash';

const { controller, get, post, put } = require('../lib/decorator');
const qiniu = require('qiniu')
@controller('/api/v0')
export class qiniuController {
    @get('/getToken')
    async getToken(ctx, next) {
        // accessKey，secretKey 在个人中心可以查看
        const accessKey = 'toY7J37YRhafqwU8wAqyDlK5jtEtgP3zW5GtmYV6';
        const secretKey = 'ynDwvoGnVhqpEUQWdR_6RqTxkdQnAA9C5zandR5o';
        // 鉴权对象 mac
        const mac = new qiniu.auth.digest.Mac(accessKey, secretKey);
        const options = {
            scope: 'h5monkey' // 仓库名
        };
        const putPolicy = new qiniu.rs.PutPolicy(options);
        const token = putPolicy.uploadToken(mac);
        const key = +new Date() + Math.random().toString(16).slice(2); // key 只需要随机不重复就可以
        ctx.body = { status: 1, data: { token, key } };
    }
    @get('/getList')
    async getList(ctx) {
        // accessKey，secretKey 在个人中心可以查看
        const accessKey = 'toY7J37YRhafqwU8wAqyDlK5jtEtgP3zW5GtmYV6';
        const secretKey = 'ynDwvoGnVhqpEUQWdR_6RqTxkdQnAA9C5zandR5o';
        var mac = new qiniu.auth.digest.Mac(accessKey, secretKey);
        var config = new qiniu.conf.Config();
        // config.useHttpsDomain = true;
        config.zone = qiniu.zone.Zone_z0;
        var bucketManager = new qiniu.rs.BucketManager(mac, config);
        var srcBucket = "h5monkey";
        // @param options 列举操作的可选参数
        //                prefix    列举的文件前缀
        //                marker    上一次列举返回的位置标记，作为本次列举的起点信息
        //                limit     每次返回的最大列举文件数量
        //                delimiter 指定目录分隔符
        var options = {
            limit: 2
        };
        const res = await asyncGetListPrefixV2(bucketManager,srcBucket,options)
        console.log('res :>> ', res);
        // bucketManager.listPrefixV2(srcBucket, options,(err, respBody, respInfo)=> {
        //     // the irregular data return from Server that Cannot be converted by urllib to JSON Object
        //     // so err !=null and you can judge if err.res.statusCode==200
        //     if (err.res.statusCode != 200) {
        //         console.log(err);
        //         ctx.body = { status: 0, error:err };
        //         throw err;
        //     }
        //     console.log('---respBody\n' + respBody + '\n---');
        //     console.log('---respInfo\n' + JSON.stringify(respInfo) + '\n---');
        //     res = JSON.stringify(respInfo)
        // });
        ctx.body = { status: 1, data:res };

    }
}

//获取全部图片
const  asyncGetListPrefixV2 = async (bucketManager,srcBucket,options) => {
    return new Promise((reslove,reject)=>{
       return  bucketManager.listPrefixV2(srcBucket, options,(err, respBody, respInfo)=> {
            // the irregular data return from Server that Cannot be converted by urllib to JSON Object
            // so err !=null and you can judge if err.res.statusCode==200
            if (err.res.statusCode != 200) {
                console.log(err);
                reject(err)
                throw err;
            }
            console.log('---respBody\n' + respBody + '\n---');
            console.log('---respInfo\n' + JSON.stringify(respInfo) + '\n---');
            reslove(respBody)
        });
    })


}




